import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

let temp = sessionStorage.getItem("user");
let token = sessionStorage.getItem("token");

//因为token的取值不是在路由上，而是将token作为请求头字段进行传递,所以把vuex的仓库作为独立模块促成存储token和user信息
export default new Vuex.Store({
  state: {
    user:temp?JSON.parse(temp):{},//取的时候转化为对象
    token: token
  },
  getters: {
  },
  mutations: {
    setUser(state,nv){
      state.user=nv
    },
    setToken(state,nv){
      state.token=nv
    }
  },
  actions: {
    setLoginUser(store, user = {}) {
      sessionStorage.setItem("user", JSON.stringify(user))//存的时候转化为字符串
      sessionStorage.setItem("token", user.token)

      store.commit("setUser", user)
      store.commit("setToken", user.token)
    }
  },
  modules: {
  }
})
